package com.ilove.media.frame;

import java.io.IOException;

import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.util.Metrics;
import org.kalmeo.kuix.widget.Picture;
import org.kalmeo.kuix.widget.PopupBox;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.kuix.widget.ScrollPane;
import org.kalmeo.kuix.widget.Text;
import org.kalmeo.kuix.widget.Widget;
import org.kalmeo.kuix.widget.Screen.ScreenMenu;
import org.kalmeo.util.frame.Frame;
import com.ilove.core.ILOVEContants;
import com.ilove.media.model.ContentList;
import com.ilove.media.model.MediaDataProvider;
import com.ilove.model.*;

public class ContentFrame implements Frame {

	// static instance of MainFrame
	public static ContentFrame instance = new ContentFrame();
	public int id;// 0 text data from vas, 1 ringtone, 2 game ,3 wallpaper
	public String contentId;
	public String contentName;
	public String avatar;
	public PopupBox pop;
	public int currPage = 1;
	public int totalPage = 1;
	public String[] vecContent = null;
	// private String closeDivTag = "</div>";
	boolean isShort = false;
	// MainFrame's Screen
	private Screen screen;
	String message = "";
	String destination = "";

	// The Pad that hold all shopLists
	private final MediaDataProvider pad = new MediaDataProvider();

	/**
	 * Show the screen
	 */
	public void showScreen() {
		if (screen != null) {
			screen.setCurrent();
		}
	}

	public void onAdded() {
		try {
			System.gc();
			prepareContent();
			// initial currPage by 1
			currPage = 1;
			// Open the Pad
			pad.setTitle(contentName);
			// Preload the screen
			screen = Kuix.loadScreen(ILOVEContants.CONTENT_MEDIA_SCREEN, pad);
			screen.setCurrent();
			changePageValue(1);
			// Draw page area
			int screenWidth = Kuix.getCanvas().getWidth();
			ScreenMenu rightMenu = (ScreenMenu) screen.getSecondMenu();
			Metrics metrics = rightMenu.getPreferredSize(screenWidth);
			int containerWidth = screenWidth - (2 * metrics.width) - 10;
			Widget container = screen.getWidget("CTM_PAGE");
			container.setAttribute("style",
					"layout:flowlayout(true, fill);align:center;min-size:"
							+ containerWidth + " 16");
		} catch (Exception e) {
			Kuix.alert(e);
			e.printStackTrace();

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onMessage(java.lang.Object,
	 *      java.lang.Object[])
	 */
	public boolean onMessage(Object identifier, Object[] arguments) {
		if ("askQuit".equals(identifier)) {
			// show askQuit box wich call onMessage method with "quit" name and
			// null arguments
			Kuix.alert(Kuix.getMessage("ASK_QUIT"),
					KuixConstants.ALERT_QUESTION | KuixConstants.ALERT_YES
							| KuixConstants.ALERT_NO, "!exit", null);
			return false;
		} else if ("back".equals(identifier)) {
			Kuix.getFrameHandler().removeFrame(instance);
			return false;
		} else if ("download".equals(identifier)) {
			if (id == 1) {
				message = "NC " + contentId;
				destination = "7585";
			} else if (id == 2) {
				message = "GA " + contentId;
				destination = "7785";
			} else if (id == 3) {
				message = "HN " + contentId;
				destination = "7585";
			}
			Kuix
					.alert(
							"Bạn download phai tra phí (5000 đồng). Bạn có đồng ý ko?)",
							KuixConstants.ALERT_QUESTION
									| KuixConstants.ALERT_YES
									| KuixConstants.ALERT_NO, "sendsms",
							"!exit");
			return false;
		} else if ("gift".equals(identifier)) {
			SendGiftFrame.instance.id = id;
			SendGiftFrame.instance.contentId = contentId;
			Kuix.getFrameHandler().pushFrame(SendGiftFrame.instance);
			return false;
		} else if ("arrLeft".equals(identifier)) {
			if (currPage > 1)
				currPage--;
			changePageValue(currPage);
		} else if ("arrRight".equals(identifier)) {
			if (currPage < totalPage)
				currPage++;
			changePageValue(currPage);
		} else if ("sendsms".equals(identifier)) {
			new SMSender(message, destination).send();
			return false;
		}
		return true;
	}

	public void changePageValue(int page) {
		Text textPage = (Text) screen.getWidget("textPage");
		textPage.setText(page + "/" + totalPage);

		Picture pictureLeft = (Picture) screen.getWidget("pic-left");
		Picture pictureRight = (Picture) screen.getWidget("pic-right");

		try {
			if (page == 1) {
				pictureLeft.setImage(Image.createImage("/img/left-dis.png"));
			} else {
				pictureLeft.setImage(Image.createImage("/img/left.png"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (page == totalPage) {
				pictureRight.setImage(Image.createImage("/img/right-dis.png"));
			} else {
				pictureRight.setImage(Image.createImage("/img/right.png"));
			}

			ScrollPane scrollPane = (ScrollPane) screen.getWidget("SP");
			if (id == 0) {
				scrollPane.setHorizontal(false);
				System.out.println(vecContent[page - 1]);
				pad.setContent((String) vecContent[page - 1]);
			} else if (id == 3) {
				System.out.println("avatar" + avatar);
				Picture pictureavatar = (Picture) screen.getWidget("avatar");
				pictureavatar.setImage(DataAccess.getImage(avatar));
				pad.setContent("");
			} else if (id == 1) {
				scrollPane.setHorizontal(false);
				Picture pictureavatar = (Picture) screen.getWidget("avatar");
				pictureavatar.setImage(Image.createImage("/img/muzik.png"));
				pad.setContent("");
				String urlmp3 = "http://210.211.99.58:6868/Upload"
						+ vecContent[page - 1].toString().trim();
				System.out.println("url mp3" + urlmp3);
				try {
					Player p = Manager.createPlayer(urlmp3.trim());
					p.realize();
					p.start();
				} catch (MediaException me) {
					me.printStackTrace();
				}
			} else {
				System.out.println("avatar" + avatar);
				scrollPane.setHorizontal(false);
				Picture pictureavatar = (Picture) screen.getWidget("avatar");
				pictureavatar.setImage(DataAccess.getImage(avatar));
				pad.setContent((String) vecContent[page - 1]);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void prepareContent() {
		try {
			//
			ContentList content = pad.getContentData(contentId, id);
			if (id == 0) {
				String[] arrContent = DataAccess.split(content.name, "\n");
				String data = DataAccess.proccessData(arrContent, 320);
				vecContent = DataAccess.split(data, "@@@");
			} else if (id == 3) {
				avatar = "http://210.211.99.58:6868/Upload" + content.name;
			} else {
				avatar = "http://210.211.99.58:6868/Upload"
						+ content.img.trim();
				String[] arrContent = DataAccess.split(content.name, "\n");
				String data = DataAccess.proccessData(arrContent, 320);
				vecContent = DataAccess.split(data, "@@@");
			}
			if (vecContent != null)
				totalPage = vecContent.length;
			else
				totalPage = 1;

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onRemoved()
	 */
	public void onRemoved() {
		// show MainFrame screen
		try {
			ContentListFrame.instance.showScreen();
			screen.cleanUp();
			screen = null;
		} catch (Exception e) {
		}
	}

}
